﻿using Domain;
using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
	public class RolesRepository : IRolesRepository
	{
		private readonly MyDbContext myDbContext;

		public RolesRepository(MyDbContext myDbContext)
        {
			this.myDbContext = myDbContext;
		}

		public async Task<List<Roles>> GetRoles()
		{
			return await myDbContext.Roles.ToListAsync();
		}

		public async Task<Roles> GetRolesById(int uid)
		{
			UserRoles userRoles =await myDbContext.UserRolesSelect.Where(e=>e.UserID==uid).FirstOrDefaultAsync();
			Roles roles= await myDbContext.Roles.Where(e => e.RolesID == userRoles.RolesID).FirstOrDefaultAsync();
			return roles;
		}

		public async Task<bool> RolesAdd(Roles roles)
		{
			myDbContext.Roles.Add(roles);
			return await myDbContext.SaveChangesAsync() > 0;
		}

		public RolesFenYe RolesData(int page, int pageSize)
		{
			var query = myDbContext.Roles.AsQueryable();
			int totalRecords = query.Count();
			var roles = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
			return new RolesFenYe
			{
				totalRecords = totalRecords,
				roles = roles
			};
		}

		public async Task<bool> RolesDelete(int id)
		{
			Roles roles = new Roles
			{
				RolesID = id
			};
			myDbContext.Roles.Remove(roles);
			return await myDbContext.SaveChangesAsync() > 0;
		}

		public async Task<bool> RolesUpDate(Roles roles)
		{
			myDbContext.Roles.Update(roles);
			return await myDbContext.SaveChangesAsync() > 0;
		}
	}
}
